package quiz;

public class Answer {

	/* ================ STATIC METHODS AND VARIABLES ================ */

	public static final int TEXT = 0;
	public static final int IMAGE = 1;


	/**
	 * Function sets the DataBase object that will be referenced by all answers
	 * MUST BE CALLED BEFORE ANY ANSWERS ARE CREATED
	 * @param db - the DataBase object referenced by all accounts 
	 */
	public static void setDataBase(DataBase database){
		db = database;
		db.open();
	}
	public static DataBase db;


	/* ================ STATIC METHODS AND VARIABLES ================ */

	/* ================ PRIVATE INSTANCE VARIABLES ================ */


	private int id;
	private Question question;
	private String content;
	private int type;
	private boolean is_correct;
	private int priority;


	/* ================ PRIVATE INSTANCE VARIABLES ================ */
	/**
	 * Constructor for answer. These are the types:
	 * 	public static final int TEXT = 0;
	 *  public static final int FIELD = 1;
	 *  public static final int IMAGE = 2;
	 * */

	public Answer(int questionID, String content, int type, boolean is_correct, int priority) {
		this.question = db.getQuestion(questionID);
		this.content = content;
		this.type = type;
		this.is_correct = is_correct;
		this.priority = priority;
	}
	
	public Answer(Question question, String content, int type, boolean is_correct, int priority) {
		this.question = question;
		this.content = content;
		this.type = type;
		this.is_correct = is_correct;
		this.priority = priority;
	}


	/* ================ GETTERS ================ */



	public int getID() {
		return id;
	}

	public Question getQuestion() {
		return question;
	}

	public String getContent() {
		return content;
	}

	public int getType() {
		return type;
	}

	public boolean isCorrect() {
		return is_correct;
	}

	public int getPriority() {
		return priority;
	}


	/* ================ GETTERS ================ */

	/* ================ SETTERS ================ */


	public void setID(int id) {
		this.id = id;
	}

	public void setQuestion(int questionID) {
		this.question = db.getQuestion(questionID);
		db.editAnswer(this);
	}
	
	public void setQuestion(Question question) {
		this.question = question;
		db.editAnswer(this);
	}

	public void setContent(String content) {
		this.content = content;
		db.editAnswer(this);
	}

	public void setType(int type) {
		this.type = type;
		db.editAnswer(this);
	}

	public void setCorrect(boolean is_correct) {
		this.is_correct = is_correct;
		db.editAnswer(this);
	}

	public void setPriority(int priority) {
		this.priority = priority;
		db.editAnswer(this);
	}

	/* ================ SETTERS ================ */



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((content == null) ? 0 : content.hashCode());
		result = prime * result + id;
		result = prime * result + question.getID();
		result = prime * result + type;
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Answer other = (Answer) obj;
		if (content == null) {
			if (other.content != null)
				return false;
		} else if (!content.equals(other.content))
			return false;
		if (id != other.id)
			return false;
		if (question.getID() != other.question.getID())
			return false;
		if (type != other.type)
			return false;
		return true;
	}	

}
